package com.itfitness.mmap;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.os.Environment;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.itfitness.mmap.databinding.ActivityMainBinding;

import java.io.File;

public class MainActivity extends AppCompatActivity {
    private Button btOpen;
    private EditText etContent;
    private Button btWrite;
    private Button btClose;
    // Used to load the 'native-lib' library on application startup.
    static {
        System.loadLibrary("native-lib");
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btOpen = (Button) findViewById(R.id.bt_open);
        etContent = (EditText) findViewById(R.id.et_content);
        btWrite = (Button) findViewById(R.id.bt_write);
        btClose = (Button) findViewById(R.id.bt_close);
        btOpen.setOnClickListener(v->{
            mmapOpen(Environment.getExternalStorageDirectory().getAbsolutePath()+"/mmaptest.txt");
        });
        btClose.setOnClickListener(v->{
            mmapClose();
        });
        btWrite.setOnClickListener(v->{
            String content = etContent.getText().toString();
            mmapWrite(content);
            etContent.setText("");
        });
    }

    /**
     * 开启共享映射
     * @param absolutePath
     */
    public native void mmapOpen(String absolutePath);

    /**
     * 关闭共享映射
     */
    public native void mmapClose();

    /**
     * 写入数据
     * @param content
     */
    public native void mmapWrite(String content);
}